<?php

namespace App\Http\Controllers\Home;

use App\Models\Bankcard;
use App\Models\Recharge;
use App\Models\Transition;
use App\Models\User;
use App\Models\Withdraw;
use Illuminate\Http\Request;

/**
 * @group 前台接口文档
 * @title 钱包模块接口
 * @author chenke
 * @description 酒庄前台钱包板块接口
 * @header name:Token type:string require:1 desc:登录令牌
 * @header name:Lang type:string require:1 desc:语言类型(en或zn)
 *  */
class Wallet extends Base
{
    /**
     * @title 交易记录分页列表
     * @description 前台交易记录分页列表
     * @url /api/wallet/trans
     * @method GET
     * @module 钱包板块
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易数据@
     * @data number:交易金额 transtype:交易类型 paytype:支付方式 transname:交易名称 transno:交易编号 balance:交易后余额 cycle_balance:反馈钱包余额 created_at:交易时间
     */
    public function transList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $limit = $data['limit'];
            $page = $data['page'];
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $model = (new Transition())->where([
                ['uid','=',$user->id],['status','=',2]
            ]);
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->orderBy('updated_at','desc')->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json([
                'code'=>0,
                'message'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户充值订单形成
     * @description 前台用户充值
     * @url /api/wallet/recharge
     * @method POST
     * @module 充值板块
     * @author chenke
     * @param name:number type:int require:1 default: other: desc:充值金额
     * @param name:evidence type:string require:1 default: other: desc:充值凭证
     * @param name:toname type:string require:1 default: other: desc:付款对象姓名
     * @param name:paybank type:string require:1 default: other: desc:开户银行
     * @param name:bankcard type:string require:1 default: other: desc:收款银行卡号
     * @return code:错误码
     * @return message:错误提示
     * @return data:充值ID
     */
    public function recharge(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $rules = [
                'number'=>'required|string',
                'evidence'=>'required|string',
                'toname'=>'required|string',
                'paybank'=>'required|string',
                'bankcard'=>'required|string'
            ];
            $messages = [
                'number.required'=> $this->lang['REQUIRED'][$lang],
                'evidence.required'=> $this->lang['REQUIRED'][$lang],
                'toname.required'=> $this->lang['REQUIRED'][$lang],
                'paybank.required'=> $this->lang['REQUIRED'][$lang],
                'bankcard.required'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $model = new Recharge();
            $model->number = $data['number'];
            $model->evidence = $data['evidence'];
            $model->toname = $data['toname'];
            $model->paybank = $data['paybank'];
            $model->bankcard = $data['bankcard'];
            $model->transtatus = 0;
            $model->uid = $user->id;
            $newToken = $this->setToken($user->id);
            $user->token = $newToken;
            $user->save();
            $res =  $model->save();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>[
                        'id'=>$model->id,
                        'number'=>$data['number'],
                        'token'=>$newToken
                    ]
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=> $e->getMessage()// $this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 充值记录列表
     * @description 前台充值记录分页列表
     * @url /api/wallet/cagelist
     * @method GET
     * @module 充值板块
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:交易数据@
     * @data number:交易金额 transtype:交易类型 paytype:支付方式 transname:交易名称 transno:交易编号 balance:交易后余额 cycle_balance:反馈钱包余额 created_at:交易时间
     */
    public function chargeList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $limit = $data['limit'];
            $page = $data['page'];
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $model = (new Recharge())->where([ ['uid','=',$user->id],['transtatus','>',0] ]);
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->orderBy('created_at','desc')->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json([
                'code'=>0,
                'message'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户提现订单形成
     * @description 前台用户提现
     * @url /api/wallet/withdraw
     * @method POST
     * @module 提现板块
     * @author chenke
     * @param name:number type:int require:1 default: other: desc:提现金额
     * @param name:cardowner type:string require:1 default: other: desc:收款人姓名
     * @param name:bankname type:string require:1 default: other: desc:开户银行
     * @param name:bankcard type:string require:1 default: other: desc:收款银行卡号
     * @return code:错误码
     * @return message:错误提示
     * @return data:提现数据@
     * @data id:提现ID number:提现金额
     */
    public function withdraw(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $rules = [
                'number'=>'required|string',
                'cardowner'=>'required|string',
                'bankname'=>'required|string',
                'bankcard'=>'required|string'
            ];
            $messages = [
                'number.required'=> $this->lang['REQUIRED'][$lang],
                'cardowner.required'=> $this->lang['REQUIRED'][$lang],
                'bankname.required'=> $this->lang['REQUIRED'][$lang],
                'bankcard.required'=> $this->lang['INTEGER'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $model = new Withdraw();
            $model->number = $data['number'];
            $model->evidence = '';
            $model->cardowner = $data['cardowner'];
            $model->bankname = $data['bankname'];
            $model->bankcard = $data['bankcard'];
            $model->transtatus = 0;
            $model->uid = $user->id;
            $newToken = $this->setToken($user->id);
            $user->token = $newToken;
            $res =  $model->save();
            $user->save();
            if($res){
                return response()->json([
                    'code'=>0,
                    'data'=>[
                        'id'=>$model->id,
                        'number'=>$data['number'],
                        'token'=>$newToken
                    ]
                ]);
            }else{
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ]);
            }
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 提现记录列表
     * @description 前台交易记录分页列表
     * @url /api/wallet/drawlist
     * @method GET
     * @module 提现板块
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:提现数据@
     * @data number:交易金额 transtype:交易类型 paytype:支付方式 transname:交易名称 transno:交易编号 balance:交易后余额 cycle_balance:反馈钱包余额 created_at:交易时间
     */
    public function withDrawList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $limit = $data['limit'];
            $page = $data['page'];
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $model = (new Withdraw())->where([ ['uid','=',$user->id],['transtatus','>',0] ]);
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->orderBy('created_at','desc')->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json([
                'code'=>0,
                'message'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 反馈钱包记录列表
     * @description 前台反馈钱包记录分页列表
     * @url /api/wallet/fanlist
     * @method GET
     * @module 钱包板块
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:提现数据@
     * @data number:交易金额 transtype:交易类型 paytype:支付方式 transname:交易名称 transno:交易编号 balance:交易后余额 cycle_balance:反馈钱包余额 created_at:交易时间
     */
    public function fanKuiList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->all();
            $limit = $data['limit'];
            $page = $data['page'];
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if(!$user){
                return response()->json([
                    'code'=>50000,
                    'message'=>$this->lang['LOGIN_EXPIRE'][$lang]
                ]);
            }
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $model = (new Transition())->where(function ($query)use ($user){
                $query->where('uid',$user->id)->where('status',2)->whereIn('transtype',[5,6]);
            })->orWhere(function ($query)use ($user){
                $query->where('uid',$user->id)->where('paytype',2)->where('status',2)->whereIn('transtype',[1,2,3]);
            });
            $offset = ($page-1) * $limit;
            $total =  $model->count();  //获取模型总数据条数
            $result = $model->offset($offset)->limit($limit)->orderBy('created_at','desc')->get()->toArray();
            $data = array(
                'total'=>$total,
                'list'=>$result
            );
            return response()->json([
                'code'=>0,
                'message'=>$data
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户银行卡列表
     * @description 前台用户银行卡列表
     * @url /api/wallet/banks
     * @method GET
     * @module 钱包板块
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:银行卡数据@
     * @data cardowner:卡主姓名 bankname:所属银行 bankcard:银行卡号
     */
    public function bankList(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $result = (new Bankcard())->where('uid',$user->id)->orderBy('created_at','desc')->get()->toArray();
            return response()->json([
                'code'=>0,
                'message'=>$result
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户银行卡绑定
     * @description 前台用户银行卡列表
     * @url /api/wallet/bindcard
     * @method POST
     * @module 钱包板块
     * @param name:cardowner type:string require:1 default: other: desc:卡主姓名
     * @param name:bankname type:string require:1 default: other: desc:所属银行
     * @param name:bankcard type:string require:1 default: other: desc:银行卡号
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:ID
     */
    public function bindBankCard(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $rules = [
                'cardowner'=>'required|string',
                'bankname'=>'required|string',
                'bankcard'=>'required|string'
            ];
            $messages = [
                'cardowner.required'=> $this->lang['REQUIRED'][$lang],
                'bankname.required'=> $this->lang['REQUIRED'][$lang],
                'bankcard.required'=> $this->lang['REQUIRED'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $model = (new Bankcard());
            $model->cardowner = $data['cardowner'];
            $model->bankname = $data['bankname'];
            $model->bankcard = $data['bankcard'];
            $model->uid = $user->id;
            $model->save();
            return response()->json([
                'code'=>0,
                'data'=>$model->id
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户银行卡编辑
     * @description 前台用户银行卡编辑
     * @url /api/wallet/edtbank
     * @method POST
     * @module 钱包板块
     * @param name:id type:int require:1 default: other: desc:银行卡ID
     * @param name:cardowner type:string require:1 default: other: desc:卡主姓名
     * @param name:bankname type:string require:1 default: other: desc:所属银行
     * @param name:bankcard type:string require:1 default: other: desc:银行卡号
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:ID
     */
    public function bankCardEdit(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $data = $request->post();
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            $rules = [
                'id'=>'required|integer',
                'cardowner'=>'required|string',
                'bankname'=>'required|string',
                'bankcard'=>'required|string'
            ];
            $messages = [
                'id.required'=> $this->lang['REQUIRED'][$lang],
                'id.integer'=> $this->lang['INTEGER'][$lang],
                'cardowner.required'=> $this->lang['REQUIRED'][$lang],
                'bankname.required'=> $this->lang['REQUIRED'][$lang],
                'bankcard.required'=> $this->lang['REQUIRED'][$lang]
            ];
            $check = $this->checkParams($data,$rules,$messages);
            if($check['code']){
                return response()->json($check);
            }
            $bank = Bankcard::find($data['id']);
            if(!$bank){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['DATA_NOT'][$lang]
                ]);
            }
            $bank->cardowner = $data['cardowner'];
            $bank->bankname = $data['bankname'];
            $bank->bankcard = $data['bankcard'];
            $bank->save();
            return response()->json([
                'code'=>0,
                'data'=>$bank->id
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }

    /**
     * @title 用户银行卡解绑
     * @description 前台用户银行卡解绑
     * @url /api/wallet/delbank
     * @method POST
     * @module 钱包板块
     * @param name:id type:int require:1 default: other: desc:银行卡ID
     * @author chenke
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function bankCardDelete(Request $request){
        $lang = $request->header('lang')?$request->header('lang'):'zn';
        try {
            $id = $request->post('id');
            if(!$id){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['DATA_NOT'][$lang]
                ]);
            }
            $user = (new User())->where('token',$request->token)->select('id','deleted_at')->first();
            if($user->deleted_at){
                return response()->json([
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ]);
            }
            (new Bankcard())->where('id',$id)->delete();
            return response()->json([
                'code'=>0,
                'data'=>''
            ]);
        }catch (\Exception $e){
            return response()->json([
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ]);
        }
    }
}
